package com.lenovo.order_service.controller;

import cn.hutool.json.JSONUtil;
import com.lenovo.order_service.api.req.*;
import com.lenovo.order_service.api.res.CreateOrderResVo;
import com.lenovo.order_service.api.res.FilterOrderResVo;
import com.lenovo.order_service.api.res.PageQueryOrderListResVo;
import com.lenovo.order_service.api.res.QueryOrderDetailResVo;
import com.lenovo.order_service.exception.BusinessException;
import com.lenovo.order_service.service.OrderService;
import com.lenovo.order_service.utils.BaseResponse;
import com.lenovo.order_service.utils.BaseResponseUtil;
import com.lenovo.order_service.utils.PageBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;



@RestController
@RequestMapping("/orders")
public class OrderController {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderService orderService;

    /**
     * 大数据量请求优化方案
     *  1.限流策略(滑动窗口、令牌桶)、熔断、降级
     *  2.考虑将订单信息放入缓存中,编辑/删除订单时,删除缓存信息或重建缓存
     *  3.优化索引
     *  4.下单多模块可以使用MQ异步处理请求
     */

    @PostMapping("createOrder")
    public BaseResponse<CreateOrderResVo> createOrder(@RequestBody @Validated CreateOrderReqVo condition) {
        try {
            LOGGER.info("createOrder,创建订单,请求参数:{}", JSONUtil.toJsonStr(condition));
            CreateOrderResVo result = orderService.createOrder(condition);
            LOGGER.info("createOrder,创建订单,返回结果:{}", JSONUtil.toJsonStr(result));
            return BaseResponseUtil.getBaseResponse(result);
        } catch (BusinessException e) {
            LOGGER.error("createOrder,创建订单失败,参数:{},业务异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("createOrder,创建订单失败,参数:{},系统异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        }
    }


    @PostMapping("queryOrderDetail")
    public BaseResponse<QueryOrderDetailResVo> queryOrderDetail(@RequestBody @Validated QueryOrderDetailReqVo condition) {
        try {
            QueryOrderDetailResVo result = orderService.queryOrderDetail(condition);
            return BaseResponseUtil.getBaseResponse(result);
        } catch (BusinessException e) {
            LOGGER.error("queryOrderDetail,查询订单详情,参数:{},业务异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("queryOrderDetail,查询订单详情,参数:{},系统异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        }
    }


    @PostMapping("pageQueryOrderList")
    public BaseResponse<PageBeanUtil<PageQueryOrderListResVo>> pageQueryOrderList(@RequestBody PageQueryOrderListReqVo condition) {
        try {
            PageBeanUtil<PageQueryOrderListResVo> result = orderService.pageQueryOrderList(condition);
            return BaseResponseUtil.getBaseResponse(result);
        } catch (BusinessException e) {
            LOGGER.error("pageQueryOrderList,分页查询订单列表,参数:{},业务异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("pageQueryOrderList,分页查询订单列表,参数:{},系统异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        }
    }


    @PostMapping("updateOrderDetail")
    public BaseResponse<Boolean> updateOrderDetail(@RequestBody @Validated UpdateOrderDetailReqVo condition) {
        try {
            orderService.updateOrderDetail(condition);
            return BaseResponseUtil.getBaseResponse(Boolean.TRUE);
        } catch (BusinessException e) {
            LOGGER.error("updateOrderDetail,更新订单信息,参数:{},业务异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("updateOrderDetail,更新订单信息,参数:{},系统异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        }
    }


    @PostMapping("deleteOrderDetail")
    public BaseResponse<Boolean> deleteOrderDetail(@RequestBody @Validated DeleteOrderDetailReqVo condition) {
        try {
            orderService.deleteOrderDetail(condition);
            return BaseResponseUtil.getBaseResponse(Boolean.TRUE);
        } catch (BusinessException e) {
            LOGGER.error("deleteOrderDetail,删除订单信息,参数:{},业务异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("deleteOrderDetail,删除订单信息,参数:{},系统异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        }
    }


    @PostMapping("filterOrderDetail")
    public BaseResponse<List<FilterOrderResVo>> filterOrderDetail(@RequestBody FilterOrderReqVo condition) {
        try {
            List<FilterOrderResVo> result = orderService.filterOrderDetail(condition);
            return BaseResponseUtil.getBaseResponse(result);
        } catch (BusinessException e) {
            LOGGER.error("filterOrderDetail,筛选订单信息,参数:{},业务异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        } catch (Exception e) {
            LOGGER.error("filterOrderDetail,筛选订单信息,参数:{},系统异常:{}", JSONUtil.toJsonStr(condition), e.getMessage());
            throw e;
        }
    }
}
